package lab2;
/*  $Id: Lab2.java
 *  
 *  Copyright 2012, The Johns Hopkins University Whiting School of Engineering
 *  All rights reserved.
 *  This material may be used, modified and reproduced by faculty,
 *  staff, and students of The Johns Hopkins University for instruction, 
 *  evaluation, and grading purposes.  For any other permission, please 
 *  contact The Johns Hopkins University Whiting School of Engineering.
 */
import java.io.*;
import java.util.*;

/**
*  Lab1 2: Main Class
*  @date       15JULY2012
*  @author     Michael W. Mathes
* 
*  @Source Reference: 
*  Reges, Stuart, and Marty Stepp. "Chapter 7.4 / Multidimensional Arrays."
*  Building Java Programs. 1st ed. Boston,MA: Pearson Education, 2008. 408-30. Print.

*/
public class Lab2 {
  public static void main(String[] args) throws FileNotFoundException {
    //  Check for valid command line arguments.
    if (args.length != 1) {
      System.out.println("Usage:  java Lab2 [input file pathname]");
      System.exit(1);
    }
        
   Scanner input = new Scanner(new File(args[0]));
   calcDet determinant = new calcDet();
   
   // for all the lines in the file
   while(input.hasNextLine()){
   int order = 0;
       
    //read first line: order of the matrix
    //convert string input to int;
    String text = input.nextLine();
    Scanner firstln = new Scanner(text);     
    order = firstln.nextInt();     

    // create a 2D matrix that will hold the matrix of size [order]x[order] 
    int[][] trinity = new int[order][order];
     
    // populate the matrix move right to left, then down
    // columns:
    for (int i=0; i< order; i++){
      // read in a new line (row)

        


        String nextln = input.nextLine();
        Scanner next = new Scanner(nextln);
      
      /** 
       * Check error condition of too many rows
       * 2
       * 1 1
       * 1 1
       * 2 2 <-- too many rows
       * 3
       * 3 3 3
       * (etc)
       * 
       * so were on the last line, and we have 
       * 
       */
      if (i == order && input.hasNextLine()){
      
      // if there is more then one item on the next line, then its an error
      // create a new scanner with the nextline    
        Scanner check = new Scanner(nextln);
      // throw away the first value  
        int blank = check.nextInt();
       System.out.println("Blank: " + blank);
      // this should return false under normal orders
      // and true if there are too many rows
        if (check.hasNextInt()){
          System.err.println("Error: Too many rows in this matrix");           
        }
      }
      
      
      // coulmns: read in each column value and store into current row + increment column
      for (int y=0; y< order; y++){
        trinity[i][y] = next.nextInt();
        
        /**
         * Check error input condition of too many columns:
         * Such as the entry in the [1][3] position below
         * 2
         * 1 1 1
         * 2 2 2 2 
         */ 
        if( y == (order-1) && next.hasNextInt()){
          System.err.println("Error: Too Many Columns in this matrix");
        }
      }
    }

       // Print out the matrix!  
       System.out.println(Arrays.deepToString(trinity));
       
       // now you have a 2d matrix 
       // calculate determinant and print
       // then profit
      
      System.out.println( determinant.det(trinity));
       
      
       
  } 
  }//endmain
}//endclass


/*  Scratchpad
 *  System.out.println("i:" + i + " order:" + order + " has next? " + input.hasNextLine() );    
 *  System.out.println("i:" + i + " order:" + order + " has next? " + input.hasNextLine());   
 * 
 */